printhdr "Starting WillowS installer"

if [ "${AUTO_PARTITION}" = "yes" ]; then
printhdr "Partitioning drive(s)"
if [ "${BOOT_TYPE}" = "UEFI" ]; then
    sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk "${DEVICE}"
        o # clear the in memory partition table
        g # make gpt table
        n # new partition
        1 # partition number 1
            # default - start at beginning of disk
        +200M # 200 MB boot parttion
        t # make partition 1 and efi partition
        1 # efi
        n # new partition
        2 # partion number 2
            # default, start immediately after preceding partition
            # default, extend partition to end of disk
        p # print the in-memory partition table
        w # write the partition table
EOF
else
    sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk "${DEVICE}"
        o # clear the in memory partition table
        n # new partition
        p # primary partition
        1 # partition number 1
            # default - start at beginning of disk
        +200M # 200 MB boot parttion
        n # new partition
        p # primary partition
        2 # partion number 2
            # default, start immediately after preceding partition
            # default, extend partition to end of disk
        a # make a partition bootable
        1 # bootable partition is partition 1
        p # print the in-memory partition table
        w # write the partition table
        q # and we're done
EOF
fi
fi
 
printhdr "Encrypting root drive"
cryptsetup luksFormat --type=luks1 "${ROOT}"
cryptsetup open "${ROOT}" cryptroot

# LVM stuff
#pvcreate /dev/mapper/cryptroot
#vgcreate vg0 /dev/mapper/cryptroot
#lvcreate -l +100%FREE vg0 --name root

printhdr "Formating partitions"
mkfs.vfat -n BOOT -F 32 "${BOOT}" # Does anything other then vfat work here?
mkfs.btrfs -L ROOT /dev/mapper/cryptroot

printhdr "Creating subvolumes"
mount -o "${FS_OPTS}" /dev/mapper/cryptroot /mnt
btrfs subvolume create /mnt/@
btrfs subvolume create /mnt/@home
btrfs subvolume create /mnt/@snapshots
umount /mnt

printhdr "Mount subvolumes"
mkdir -p /mnt
mount -o "${FS_OPTS}",subvol=@ /dev/mapper/cryptroot /mnt
mkdir -p /mnt/home
mount -o "${FS_OPTS}",subvol=@home /dev/mapper/cryptroot /mnt/home
mkdir -p /mnt/.snapshots
mount -o "${FS_OPTS}",subvol=@snapshots /dev/mapper/cryptroot /mnt/.snapshots

# There's a better way to do this I think
printhdr "Mounting boot partion"
mkdir -p /mnt/boot/efi
mount -o rw,noatime "${BOOT}" /mnt/boot/efi

printhdr "Excluding directories from btrfs"
[ ! -d /mnt/var ] && mkdir -p /mnt/var
[ -d /mnt/var/cache ] && rm -rf /mnt/var/cache
btrfs subvolume create /mnt/var/cache
[ -d /mnt/var/tmp ] && rm -rf /mnt/var/tmp
btrfs subvolume create /mnt/var/tmp
[ -d /mnt/srv ] && rm -rf /mnt/srv
btrfs subvolume create /mnt/srv

# What are these for?
printhdr "Mount extra directories..."
mkdir -p /mnt/dev/
mount -o bind /dev /mnt/dev/
mkdir -p /mnt/proc/
mount -t proc none /mnt/proc/
mkdir -p  /mnt/sys/
mount -o bind /sys /mnt/sys/
mkdir -p /mnt/dev/pts/
mount -t devpts pts /mnt/dev/pts/

printhdr "Adding repos (dry run)"
echo "XBPS_ARCH=${ARCH} xbps-install -Synuv -R ${MIRROR} -r /mnt $(awk \"{NF}\" <(echo ${REPOS}))"
XBPS_ARCH=${ARCH} xbps-install -Synuv -R ${MIRROR} -r /mnt $(awk "{NF}" <(echo ${REPOS}))
printhdr "Adding repos"
echo "XBPS_ARCH=${ARCH} xbps-install -Suv -R ${MIRROR} -r /mnt $(awk \"{NF}\" <(echo ${REPOS}))"
XBPS_ARCH=${ARCH} xbps-install -Suv -R ${MIRROR} -r /mnt $(awk "{NF}" <(echo ${REPOS}))

printhdr "Installing packages (dry run)"
echo "XBPS_ARCH=${ARCH} xbps-install -Synuv -R ${MIRROR} -r /mnt $(awk \"{NF}\" <(echo ${PACKAGES}))"
XBPS_ARCH=${ARCH} xbps-install -Synuv -R ${MIRROR} -r /mnt $(awk "{NF}" <(echo ${PACKAGES}))
printhdr "Installing packages"
echo "XBPS_ARCH=${ARCH} xbps-install -Suv -R ${MIRROR} -r /mnt $(awk \"{NF}\" <(echo ${PACKAGES}))"
XBPS_ARCH=${ARCH} xbps-install -Suv -R ${MIRROR} -r /mnt $(awk "{NF}" <(echo ${PACKAGES}))

printhdr "Copying network info"
mkdir -p /mnt/etc
cp -L /etc/resolv.conf /mnt/etc/

wget https://my-artifacts.us-east-1.linodeobjects.com/willows-"${VERSION}".tar.gz

tar xvf willows-"${VERSION}".tar.gz -C /mnt/

PS1="(chroot) $ " SHELL=/bin/bash chroot /mnt /willows/install-willows /willows/config.yaml
